<template>
  <div class="goods-list">
    <myheader outerTitle="商品列表"></myheader>
    <ul>
      <!--  router-link有个tag属性，用于指定router-link渲染成的标签 -->
      <router-link class="goods-li" :to="`/goodsDetail/${item.id}`" v-for="item in goodsList" :key="item.id" tag="li">
        <div class="goods-container">
          <img :src="item.img_url" :alt="item.title">
          <h4>{{item.title}}</h4>
          <div class="goods-info">
            <div class="price-info">
              <span class="price-new">{{item.sell_price}}</span>
              <span class="price-old">{{item.market_price}}</span>
            </div>
            <div class="sell-info">
              <span class="sell-status">热卖中</span>
              <span class="sell-leftcount">{{item.stock_price}}</span>
            </div>
          </div>
        </div>
      </router-link>
    </ul>
  </div>
</template>
<script>
import myheader from '@/components/myheader'
import { getGoodsList } from '@/api'
export default {
  data () {
    return {
      goodsList: {},
      pageindex: 1 // 默认渲染第一页的数据
    }
  },
  components: {
    myheader
  },
  created () {
    this.initGoodsList()
  },
  methods: {
    initGoodsList () {
      getGoodsList(this.pageindex)
        .then(res => {
        // console.log(res)
          this.goodsList = res.message
        })
    }
  }
}
</script>
<style lang="scss" scoped>
  .goods-list {
  margin: 40px 0 0 0;
  background-color: #f4f4f4;
  ul {
    display: flex;
    flex-wrap: wrap;
    font-size: 10px;
  }
  h4 {
    font-size: 12px;
    font-weight: bold;
    height: 32px;
    // 多行文本超出省略号
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
  }
  .goods-li {
    width: 50%;
    box-sizing: border-box;
    padding-top: 5px;
    display: flex;
    justify-content: column;
    .goods-container {
      width: 100%;
      background-color: #fff;
      padding: 5px;
      display: flex;
      flex-direction: column;
      justify-content: center;
    }
    .price-info {
      display: flex;
      justify-content: space-between;
      .price-new {
        color: red;
      }
      .price-old {
        text-decoration: line-through;
      }
    }
    .sell-info {
      display: flex;
      justify-content: space-between;
    }
  }

  .goods-li:nth-child(2n+1) {
    padding-right: 5px;
  }
  .goods-li:nth-child(2n) {
    padding-left: 5px;
  }
}
</style>
